#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import dbus
import dbus.service
import dbus.glib
import gobject
import os
from os.path import join, getsize

#Capturem les cameres que soporten video4linux
def getCamerasV4L():
	cameras={}
	bus = dbus.SystemBus()
	hal_manager_obj = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager")
	hal_manager = dbus.Interface(hal_manager_obj, "org.freedesktop.Hal.Manager")
	device_names = hal_manager.GetAllDevices()
	for device in device_names:
		try:
			device_obj = bus.get_object("org.freedesktop.Hal", device)
			category = device_obj.GetProperty("info.category", dbus_interface="org.freedesktop.Hal.Device")
			if (category == "video4linux"):
				path = device_obj.GetProperty("video4linux.device", dbus_interface="org.freedesktop.Hal.Device")
				cameras[device]=path
		except:
			pass   
	return cameras

#Capturem cameres que no soporten video4linux
def getCameras(directory, cameras):
	for root, dirs, files in os.walk(directory):
		if (dirs != []):
			for d in dirs:
				getCameras(d, cameras)
		else:
			try:
				myfile = open(root + "/" + files[0], 'r')
				myfile.readline()
				myfile.readline()
				myfile.readline()
				line = myfile.readline()
				identifier=line.replace('Device Id        :','')
				identifier = identifier.replace('\n','')
				cameras[identifier] = root + "/" + files[0]
				print cameras[identifier] 
			except:
				pass
	return cameras
